//
//  SicknessPickViewController.m
//  QuickCure1
//
//  Created by Haozhong Chen on 2016/11/2.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "SicknessPickViewController.h"
#import "Disease.h"
#import "SicknessComplicationData.h"

@interface SicknessPickViewController ()

@property(nonatomic,strong)NSMutableArray<SicknessComplicationData*>*selectedComplicationList;

@end


static NSString*CellID=@"CellID";

@implementation SicknessPickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellID];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(NSMutableArray<SicknessComplicationData*>*)selectedComplicationList
{
    if (!_selectedComplicationList)
    {
        _selectedComplicationList=[[NSMutableArray alloc]initWithCapacity:2];
    }
    return _selectedComplicationList;
}


-(void)setupUI
{
    if (_isClickedComplication)
    {
        UIButton *selectedBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        selectedBtn.frame = CGRectMake(0, 0, 60, 30);
        
        [selectedBtn setTitle:@"完成" forState:UIControlStateNormal];
        [selectedBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [selectedBtn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *selectItem = [[UIBarButtonItem alloc] initWithCustomView:selectedBtn];
        
        self.navigationItem.rightBarButtonItem =selectItem;
        
        self.tableView.allowsMultipleSelectionDuringEditing = YES;
        
        self.tableView.editing = YES;
    }
 
}


#pragma mark - 选择完成的按钮点击事件
-(void)selectedBtn:(UIButton*)selectedBtn
{
//     [self.delegate pickedComplication:data];
     [self.navigationController popViewControllerAnimated:YES];
    [self.delegate pickedComplicationList:self.selectedComplicationList];
     _isClickedComplication=NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 返回行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (_isClickedComplication)
    {
        
        return self.complicationDataList.count;
    }
    else if(_isClickedDiease)
    {
         return self.dataList.count;
    }
    else
        return 2;
    
   
}


#pragma mark - 返回单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    if (_isClickedComplication)
    {
        cell.textLabel.text=self.complicationDataList[indexPath.row].complication_name;
        
        return cell;
       
    }
    else if(_isClickedDiease)
    {
        cell.textLabel.text=self.dataList[indexPath.row].ci3_name;
        
        return cell;
    }
    else
    {
        if (indexPath.row==0)
        {
            cell.textLabel.text=@"保守治疗";
        }
        else
        {
            cell.textLabel.text=@"先花他一个亿";
        }
        return cell;
    }
    
}

#pragma mark - 选中单元格的代理方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isClickedComplication)
    {
        SicknessComplicationData*data=self.complicationDataList[indexPath.row];
        
        [self.selectedComplicationList addObject:data];
        
        if (self.selectedComplicationList.count==2)
        {
            _isClickedComplication=NO;
            [self.navigationController popViewControllerAnimated:YES];
            [self.delegate pickedComplicationList:self.selectedComplicationList.copy];
        }
//        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    }
    else if(_isClickedDiease)
    {
        Disease*data=self.dataList[indexPath.row];
        [self.delegate pickedDiease:data];
        
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        BOOL isConservativeTreat;
        if(indexPath.row==0)
            isConservativeTreat=YES;
        else
            isConservativeTreat=NO;
        
        [self.delegate pickedTreatment:isConservativeTreat];
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [self.navigationController popViewControllerAnimated:YES];

    }
    
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
